// VBSessionServer.cpp
// www.vbmcgi.org

// experimental, not to be used begin
#if 0

#include "VBMcgi/VBSessionServer.h"
#include "ptypes.h"

using namespace br::com::sbVB::VBLib;
using namespace br::com::sbVB::VBMcgi;
using namespace std;


// default constructor
VBSessionServer::VBSessionServer()
{
	m_time = time(NULL); // get system time in unsigned format
	srand (m_time); // do randomize
}

// destructor
VBSessionServer::~VBSessionServer()
{
}

// return true if session exists
bool VBSessionServer::existSession(const char * sessionID)
{
	AllDataTypeIterator it = m_data.find(sessionID);
	return it!=m_data.end();
}

// deletes an existing session and all its variables
bool VBSessionServer::deleteSesssion(const char * sessionID)
{
	AllDataTypeIterator it = m_data.find(sessionID);
	if (it!=m_data.end()) 
	{
		m_data.erase(it);
		return true;
	}
	else 
		return false;
}


void VBSessionServer::addVariable(const char * sessionID, const char * varName, const char * value)
{
	// prepara data for insertion
	SessionDataTypePair data(varName,value);

	AllDataTypeIterator it = m_data.find(sessionID);
	if (it!=m_data.end())  // if (existession(sessionID)
	{	// session not empty (there's already a SessionData object in the m_SessionDataContainer
		// cout << "DEBUG session not empty" << endl;
		it->second.insert(data); // insert data to section already selected
	}
	else 
	{	// session is empty
		// cout << "DEBUG session empty" << endl;
		SessionDataType sessionData; // create new session data
		sessionData.insert(data); // insert data to new session dat
		AllDataTypePair session(sessionID,sessionData); 
		m_data.insert(session); // add all data pair
	}
}

VBString VBSessionServer::getVarValue(const char * sessionID, const char * varName)
{
	AllDataTypeIterator it = m_data.find(sessionID);
	if (it == m_data.end()) // // if (!existession(sessionID))
		throw VBSessionServerException("this section does not exist");

	SessionDataTypeIterator it_session = it->second.find(varName);
	return it_session->second;
}

// debug methods
VBString VBSessionServer::listSession(AllDataTypeIterator it)
{
	VBString ret="";
	int c = 0;
	for (SessionDataTypeIterator it_session = it->second.begin();
		it_session != it->second.end();
		it_session++)
		{
			ret+="|";
			ret+=it_session->first;
			ret+="|";
			ret+=it_session->second;
			//cout << "Variable " << ++c << " : " << it_session->first;
			//cout << ", Value :" << it_session->second << endl;
			
		}
	return ret;
}

VBString VBSessionServer::listSessionContainers(bool hideVar, const char *sID)
{
	VBString ret="";
	int c = 0;
	for (AllDataTypeIterator it = m_data.begin();
		it != m_data.end();
		it++)
		{
			//cout << "Data container " << ++c << " : " << it->first << endl;
			VBString deb=it->first;
			if(hideVar!=true)
			{
				if (it->first==sID)
					ret=listSession(it);
			}
			else
			{
				ret+="|";
				ret+=it->first;	
			
			}
		}
	return ret;
}

VBString VBSessionServer::createSession()
{
	bool status = false;
	VBString aux;
	do
	{
		// randomize
		srand((unsigned)time(NULL));

		// get random number
		aux=rand();

		// DEBUG
		//cout<< "DEBUG: ID " << aux << endl;
		// --
		if (!(existSession(aux)))
		{
			//m_sessionID = aux;
			status = true;
		}
	} while(status == false);

	addVariable(aux,"___VARNAME__","__VALUE__");
	
	//return m_sessionID;
	return aux;
}

///////////////////////////////////////////////////////

#endif
// experimental, not to be used end

